package policy.nodes;

import java.util.ArrayList;
import java.util.Collections;

import policy.branches.Branch;

public class Node {
	private String type = "";
	private ArrayList<Branch> branches = new ArrayList<Branch>();

	public Node(String type) {
		this.type = type;
	}

	public String getType() {
		return type;
	}

	public ArrayList<Branch> getBranches() {
		return branches;
	}

	/**
	 * Adds a Branch to this Node. A TreeSet is used to maintain the proper
	 * order of the Branches.
	 * 
	 * @param b
	 *            the Branch to add.
	 * @return the Node at the end of this Branch. This is required in case the
	 *         Branch already exists, allowing us to get the reference to the
	 *         already existing Node.
	 */
	public Node addBranch(Branch b) {
		for (int i=0; i<branches.size(); i++) {
			if (branches.get(i).equals(b))
				return branches.get(i).getNode();
		}
		branches.add(b);
		Collections.sort(branches);
		return b.getNode();
	}
}